package com.sdp.lease.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sdp.core.bizc.db.QueryColumn;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.lease.db.l_banner_info.service.ILBannerInfoService;
import com.sdp.lease.db.l_eq_lease_info.entity.LEqLeaseInfo;
import com.sdp.lease.db.l_eq_type.entity.LEqType;
import com.sdp.lease.db.l_eq_type.service.ILEqTypeService;
import com.sdp.lease.db.l_login_info.entity.LLoginInfo;
import com.sdp.lease.db.l_supplier_apply.entity.LSupplierApply;
import com.sdp.lease.db.l_supplier_apply.service.ILSupplierApplyService;
import com.sdp.lease.db.l_tenant_apply_info.entity.LTenantApplyInfo;
import com.sdp.lease.db.l_tenant_apply_info.service.ILTenantApplyInfoService;
import com.sdp.lease.db.l_wz_lease_info.entity.LWzLeaseInfo;
import com.sdp.lease.db.l_wz_type.entity.LWzType;
import com.sdp.lease.db.l_wz_type.service.ILWzTypeService;
import com.sdp.lease.utils.LeaseConstants;
import com.sdp.lease.utils.WxApi;
import com.sdp.right.control.SysDictController;

@RestController
@RequestMapping("/LeaseWeiXinController")
public class LeaseWeiXinController {

	@Autowired
	private SysDictController sysDictController;
	
	@Autowired
	private LBannerInfoController lBannerInfoController;
	
	@Autowired
	private LNoticeInfoController lNoticeInfoController;
	
	@Autowired
	private LLoginInfoController lLoginInfoController;
	
	@Autowired
	private LSupplierApplyController lSupplierApplyController;
	
	@Autowired
	private LTenantApplyInfoController lTenantApplyInfoController;
	
	@Autowired
	private ILSupplierApplyService iLSupplierApplyService;
	
	@Autowired
	private ILTenantApplyInfoService iLTenantApplyInfoService;
	
	@Autowired
	private LEqLeaseInfoController lEqLeaseInfoController;
	
	@Autowired
	private LWzLeaseInfoController lWzLeaseInfoController;
	
	@Autowired
	private ILEqTypeService iLEqTypeService;
	
	@Autowired
	private LEqInfoController lEqInfoController;
	
	@Autowired
	private ILWzTypeService iLWzTypeService;
	
	@Autowired
	private LWzInfoController lWzInfoController;
	
	@Autowired
	private LSysConfigController lSysConfigController;
	
	@Autowired
	private ILBannerInfoService iLBannerInfoService;
	
	
	/**
	 * 根据字典编号获取字典信息
	 * @param dictCode
	 * @return
	 */
	@GetMapping(value = "/getDictInfo")
	public Result<?> getDictInfo(@RequestParam(name="dictCode",required=true) String dictCode,@RequestParam(name="dictParam",defaultValue="") String dictParam) {
		return sysDictController.getDictInfo(dictCode, dictParam);
	}
	
	/**
	 * 查询最新5条轮播图信息
	 * @return
	 */
	@GetMapping(value = "/queryTopBannerInfo")
	public Result<?> queryTopBannerInfo() {
		DataCenter dataCenter = new DataCenter();
		dataCenter.setPageNo(1);
		dataCenter.setPageSize(5);
		List<QueryColumn> orderColumns = new ArrayList<>();
		QueryColumn orderColumn = new QueryColumn();
		orderColumn.setField("createTime");
		orderColumn.setVal("desc");
		orderColumns.add(orderColumn);
		dataCenter.setOrderColumns(orderColumns);
		
		List<QueryColumn> queryColumns = new ArrayList<>();
		QueryColumn queryColumn = new QueryColumn();
		queryColumn.setField("isUse");
		queryColumn.setVal(LeaseConstants.是.getCode());
		queryColumns.add(queryColumn);
		dataCenter.setQueryForm(queryColumns);
		
		return lBannerInfoController.page(dataCenter);
	}
	
	/**
	 * 分页查询轮播图信息
	 * @param dataCenter
	 * @return
	 */
	@GetMapping(value = "/queryPageBannerInfo")
	public Result<?> queryPageBannerInfo(@RequestParam(name="pageNo",required=true) String pageNo) {
		DataCenter dataCenter = new DataCenter();
		dataCenter.setPageNo(Integer.parseInt(pageNo));
		dataCenter.setPageSize(15);
		List<QueryColumn> orderColumns = new ArrayList<>();
		QueryColumn orderColumn = new QueryColumn();
		orderColumn.setField("createTime");
		orderColumn.setVal("desc");
		orderColumns.add(orderColumn);
		dataCenter.setOrderColumns(orderColumns);
		
		List<QueryColumn> queryColumns = new ArrayList<>();
		QueryColumn queryColumn = new QueryColumn();
		queryColumn.setField("isUse");
		queryColumn.setVal(LeaseConstants.是.getCode());
		queryColumns.add(queryColumn);
		dataCenter.setQueryForm(queryColumns);
		
		return lBannerInfoController.page(dataCenter);
	}
	
	
	/**
	 * 根据内码查询轮播图详情
	 * @param keyId
	 * @return
	 */
	@GetMapping(value = "/queryBannerInfoById")
	public Result<?> queryBannerInfoById(@RequestParam(name="keyId",required=true) String keyId) {
		return Result.ok(iLBannerInfoService.getById(keyId));
	}
	
	/**
	 * 查询最新5条公告信息
	 * @return
	 */
	@GetMapping(value = "/queryTopNoticeInfo")
	public Result<?> queryTopNoticeInfo() {
		DataCenter dataCenter = new DataCenter();
		dataCenter.setPageNo(1);
		dataCenter.setPageSize(5);
		List<QueryColumn> orderColumns = new ArrayList<>();
		QueryColumn orderColumn = new QueryColumn();
		orderColumn.setField("createTime");
		orderColumn.setVal("desc");
		orderColumns.add(orderColumn);
		dataCenter.setOrderColumns(orderColumns);
		
		List<QueryColumn> queryColumns = new ArrayList<>();
		QueryColumn queryColumn = new QueryColumn();
		queryColumn.setField("isUse");
		queryColumn.setVal(LeaseConstants.是.getCode());
		queryColumns.add(queryColumn);
		dataCenter.setQueryForm(queryColumns);
		
		return lNoticeInfoController.page(dataCenter);
	}
	
	/**
	 * 分页查询公告信息
	 * @param dataCenter
	 * @return
	 */
	@GetMapping(value = "/queryPageNoticeInfo")
	public Result<?> queryPageNoticeInfo(@RequestParam(name="pageNo",required=true) String pageNo) {
		DataCenter dataCenter = new DataCenter();
		dataCenter.setPageNo(Integer.parseInt(pageNo));
		dataCenter.setPageSize(15);
		List<QueryColumn> orderColumns = new ArrayList<>();
		QueryColumn orderColumn = new QueryColumn();
		orderColumn.setField("createTime");
		orderColumn.setVal("desc");
		orderColumns.add(orderColumn);
		dataCenter.setOrderColumns(orderColumns);
		
		List<QueryColumn> queryColumns = new ArrayList<>();
		QueryColumn queryColumn = new QueryColumn();
		queryColumn.setField("isUse");
		queryColumn.setVal(LeaseConstants.是.getCode());
		queryColumns.add(queryColumn);
		dataCenter.setQueryForm(queryColumns);
		
		return lNoticeInfoController.page(dataCenter);
	}
	
	/**
	 * 微信小程序登录，并获取手机号
	 * @param loginInfo
	 * @return
	 */
	@GetMapping(value = "/loginSave")
	public Result<?> loginSave(@RequestParam(name="code",required=true) String code) {
		String res = WxApi.getuserphonenumber(code);
    	JSONObject resObj = JSONObject.parseObject(res);
    	if("0".equals(resObj.getString("errcode"))) {
    		String phone = resObj.getJSONObject("phone_info").getString("phoneNumber");
    		LLoginInfo loginInfo = new LLoginInfo();
    		loginInfo.setAccountNo(phone);
    		lLoginInfoController.save(loginInfo);
    		return Result.ok(loginInfo);
    	}else {
    		return Result.error("获取手机号失败");
    	}
		
	}
	
	/**
	 * 供应商申请
	 * @param supplierApply
	 * @return
	 */
	@PostMapping(value = "/supplierApply")
	public Result<?> supplierApply(@RequestBody LSupplierApply supplierApply) {
		return lSupplierApplyController.save(supplierApply);
	}
	
	/**
	 * 个人中心，查询自己的供应商申请记录
	 * @param accountNo
	 * @return
	 */
	@GetMapping(value = "/queryLSupplierApplyByAccountNo")
	public Result<?> queryLSupplierApplyByAccountNo(@RequestParam(name="accountNo",required=true) String accountNo) {
		return Result.ok(iLSupplierApplyService.queryLSupplierApplyByAccountNo(accountNo));
	}

	/**
	 * 求租人申请
	 * @param tenantApplyInfo
	 * @return
	 */
	@PostMapping(value = "/tenantApply")
	public Result<?> tenantApply(@RequestBody LTenantApplyInfo tenantApplyInfo) {
		return lTenantApplyInfoController.save(tenantApplyInfo);
	}
	
	/**
	 * 个人中心，查询自己的求租人申请记录
	 * @param accountNo
	 * @return
	 */
	@GetMapping(value = "/queryTenantApplyByAccountNo")
	public Result<?> queryTenantApplyByAccountNo(@RequestParam(name="accountNo",required=true) String accountNo) {
		return Result.ok(iLTenantApplyInfoService.queryTenantApplyByAccountNo(accountNo));
	}
	
	/**
	 * 个人中心，分页查询个人设备求租信息
	 * @return
	 */
	@GetMapping(value = "/queryPageEqLeaseInfoByAccountNo")
	public Result<?> queryPageEqLeaseInfoByAccountNo(@RequestParam(name="pageNo",required=true) String pageNo,@RequestParam(name="accountNo",required=true) String accountNo) {
		DataCenter dataCenter = new DataCenter();
		dataCenter.setPageNo(Integer.parseInt(pageNo));
		dataCenter.setPageSize(15);
		List<QueryColumn> orderColumns = new ArrayList<>();
		QueryColumn orderColumn = new QueryColumn();
		orderColumn.setField("applyTime");
		orderColumn.setVal("desc");
		orderColumns.add(orderColumn);
		dataCenter.setOrderColumns(orderColumns);
		
		List<QueryColumn> queryColumns = new ArrayList<>();
		QueryColumn queryColumn = new QueryColumn();
		queryColumn.setField("tenantAccountNo");
		queryColumn.setVal(accountNo);
		queryColumns.add(queryColumn);
		dataCenter.setQueryForm(queryColumns);
		
		return lEqLeaseInfoController.page(dataCenter);
	}
	
	/**
	 * 个人中心，分页查询个人物资求购信息
	 * @return
	 */
	@GetMapping(value = "/queryPageWzLeaseInfoByAccountNo")
	public Result<?> queryPageWzLeaseInfoByAccountNo(@RequestParam(name="pageNo",required=true) String pageNo,@RequestParam(name="accountNo",required=true) String accountNo) {
		DataCenter dataCenter = new DataCenter();
		dataCenter.setPageNo(Integer.parseInt(pageNo));
		dataCenter.setPageSize(15);
		List<QueryColumn> orderColumns = new ArrayList<>();
		QueryColumn orderColumn = new QueryColumn();
		orderColumn.setField("applyTime");
		orderColumn.setVal("desc");
		orderColumns.add(orderColumn);
		dataCenter.setOrderColumns(orderColumns);
		
		List<QueryColumn> queryColumns = new ArrayList<>();
		QueryColumn queryColumn = new QueryColumn();
		queryColumn.setField("tenantAccountNo");
		queryColumn.setVal(accountNo);
		queryColumns.add(queryColumn);
		dataCenter.setQueryForm(queryColumns);
		
		return lWzLeaseInfoController.page(dataCenter);
	}
	
	
	/**
	 * 个人中心，分页查询个人物资信息
	 * @return
	 */
	@GetMapping(value = "/queryPageWzInfoByAccountNo")
	public Result<?> queryPageWzInfoByAccountNo(@RequestParam(name="pageNo",required=true) String pageNo,@RequestParam(name="accountNo",required=true) String accountNo) {
		DataCenter dataCenter = new DataCenter();
		dataCenter.setPageNo(Integer.parseInt(pageNo));
		dataCenter.setPageSize(15);
		List<QueryColumn> orderColumns = new ArrayList<>();
		QueryColumn orderColumn = new QueryColumn();
		orderColumn.setField("createTime");
		orderColumn.setVal("desc");
		orderColumns.add(orderColumn);
		dataCenter.setOrderColumns(orderColumns);
		
		List<QueryColumn> queryColumns = new ArrayList<>();
		QueryColumn queryColumn = new QueryColumn();
		queryColumn.setField("wzSupplier");
		queryColumn.setVal(accountNo);
		queryColumns.add(queryColumn);
		dataCenter.setQueryForm(queryColumns);
		
		return lWzInfoController.page(dataCenter);
	}
	
	/**
	 * 个人中心，分页查询个人设备信息
	 * @return
	 */
	@GetMapping(value = "/queryPageEqInfoByAccountNo")
	public Result<?> queryPageEqInfoByAccountNo(@RequestParam(name="pageNo",required=true) String pageNo,@RequestParam(name="accountNo",required=true) String accountNo) {
		DataCenter dataCenter = new DataCenter();
		dataCenter.setPageNo(Integer.parseInt(pageNo));
		dataCenter.setPageSize(15);
		List<QueryColumn> orderColumns = new ArrayList<>();
		QueryColumn orderColumn = new QueryColumn();
		orderColumn.setField("createTime");
		orderColumn.setVal("desc");
		orderColumns.add(orderColumn);
		dataCenter.setOrderColumns(orderColumns);
		
		List<QueryColumn> queryColumns = new ArrayList<>();
		QueryColumn queryColumn = new QueryColumn();
		queryColumn.setField("eqSupplier");
		queryColumn.setVal(accountNo);
		queryColumns.add(queryColumn);
		dataCenter.setQueryForm(queryColumns);
		
		return lEqInfoController.page(dataCenter);
	}
	
	
	/**
	 * 首页查询最新的10条设备信息
	 * @return
	 */
	@GetMapping(value = "/queryTopEqInfo")
	public Result<?> queryTopEqInfo() {
		DataCenter dataCenter = new DataCenter();
		dataCenter.setPageNo(1);
		dataCenter.setPageSize(10);
		List<QueryColumn> orderColumns = new ArrayList<>();
		QueryColumn orderColumn = new QueryColumn();
		orderColumn.setField("createTime");
		orderColumn.setVal("desc");
		orderColumns.add(orderColumn);
		dataCenter.setOrderColumns(orderColumns);
		
		List<QueryColumn> queryColumns = new ArrayList<>();
		QueryColumn queryColumn = new QueryColumn();
		queryColumn.setField("saleState");
		queryColumn.setVal(LeaseConstants.上架状态.getCode());
		queryColumns.add(queryColumn);
		
		QueryColumn queryColumn2= new QueryColumn();
		queryColumn2.setField("showHome");
		queryColumn2.setVal(LeaseConstants.是.getCode());
		queryColumns.add(queryColumn);
		
		QueryColumn queryColumn3= new QueryColumn();
		queryColumn3.setField("eqState");
		queryColumn3.setVal(LeaseConstants.设备状态_闲置.getCode());
		queryColumns.add(queryColumn);
		
		dataCenter.setQueryForm(queryColumns);
		
		return lEqInfoController.page(dataCenter);
	}
	
	
	/**
	 * 分页查询设备求租信息
	 * @return
	 */
	@GetMapping(value = "/queryPageEqLeaseInfo")
	public Result<?> queryPageEqLeaseInfo(@RequestParam(name="pageNo",required=true) String pageNo) {
		DataCenter dataCenter = new DataCenter();
		dataCenter.setPageNo(Integer.parseInt(pageNo));
		dataCenter.setPageSize(15);
		List<QueryColumn> orderColumns = new ArrayList<>();
		QueryColumn orderColumn = new QueryColumn();
		orderColumn.setField("applyTime");
		orderColumn.setVal("desc");
		orderColumns.add(orderColumn);
		dataCenter.setOrderColumns(orderColumns);
		
		List<QueryColumn> queryColumns = new ArrayList<>();
		QueryColumn queryColumn = new QueryColumn();
		queryColumn.setField("applyState");
		queryColumn.setVal(LeaseConstants.申请状态_申请通过.getCode());
		queryColumns.add(queryColumn);
		dataCenter.setQueryForm(queryColumns);
		
		return lEqLeaseInfoController.page(dataCenter);
	}
	
	
	/**
	 * 首页查询最新10条物资信息
	 * @return
	 */
	@GetMapping(value = "/queryTopWzInfo")
	public Result<?> queryTopWzInfo() {
		DataCenter dataCenter = new DataCenter();
		dataCenter.setPageNo(1);
		dataCenter.setPageSize(10);
		List<QueryColumn> orderColumns = new ArrayList<>();
		QueryColumn orderColumn = new QueryColumn();
		orderColumn.setField("createTime");
		orderColumn.setVal("desc");
		orderColumns.add(orderColumn);
		dataCenter.setOrderColumns(orderColumns);
		
		List<QueryColumn> queryColumns = new ArrayList<>();
		QueryColumn queryColumn = new QueryColumn();
		queryColumn.setField("saleState");
		queryColumn.setVal(LeaseConstants.上架状态.getCode());
		queryColumns.add(queryColumn);
		dataCenter.setQueryForm(queryColumns);
		
		QueryColumn queryColumn2= new QueryColumn();
		queryColumn2.setField("showHome");
		queryColumn2.setVal(LeaseConstants.是.getCode());
		queryColumns.add(queryColumn);
		dataCenter.setQueryForm(queryColumns);
		
		
		return lWzInfoController.page(dataCenter);
	}
	
	/**
	 * 分页查询物资求购信息
	 * @return
	 */
	@GetMapping(value = "/queryPageWzLeaseInfo")
	public Result<?> queryPageWzLeaseInfo(@RequestParam(name="pageNo",required=true) String pageNo) {
		DataCenter dataCenter = new DataCenter();
		dataCenter.setPageNo(Integer.parseInt(pageNo));
		dataCenter.setPageSize(15);
		List<QueryColumn> orderColumns = new ArrayList<>();
		QueryColumn orderColumn = new QueryColumn();
		orderColumn.setField("applyTime");
		orderColumn.setVal("desc");
		orderColumns.add(orderColumn);
		dataCenter.setOrderColumns(orderColumns);
		
		List<QueryColumn> queryColumns = new ArrayList<>();
		QueryColumn queryColumn = new QueryColumn();
		queryColumn.setField("applyState");
		queryColumn.setVal(LeaseConstants.申请状态_申请通过.getCode());
		queryColumns.add(queryColumn);
		dataCenter.setQueryForm(queryColumns);
		
		return lWzLeaseInfoController.page(dataCenter);
	}
	
	/**
	 * 查询设备分类信息
	 * @return
	 */
	@GetMapping(value = "/queryEqType")
	public Result<?> queryEqType() {
		LambdaQueryWrapper<LEqType> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(LEqType::getIsUse, LeaseConstants.是.getCode());
		queryWrapper.orderByAsc(LEqType::getTypeOrder);
		return Result.ok(iLEqTypeService.list(queryWrapper));
	}
	
	/**
	 * 根据设备分类，查询分类下的设备信息
	 * @param pageNo
	 * @param eqType
	 * @return
	 */
	@GetMapping(value = "/queryPageEqInfoByType")
	public Result<?> queryPageEqInfoByType(@RequestParam(name="pageNo",required=true) String pageNo,@RequestParam(name="eqType",required=true) String eqType) {
		
		DataCenter dataCenter = new DataCenter();
		dataCenter.setPageNo(Integer.parseInt(pageNo));
		dataCenter.setPageSize(15);
		List<QueryColumn> orderColumns = new ArrayList<>();
		QueryColumn orderColumn = new QueryColumn();
		orderColumn.setField("createTime");
		orderColumn.setVal("desc");
		orderColumns.add(orderColumn);
		dataCenter.setOrderColumns(orderColumns);
		
		List<QueryColumn> queryColumns = new ArrayList<>();
		QueryColumn queryColumn = new QueryColumn();
		queryColumn.setField("eqType");
		queryColumn.setVal(eqType);
		queryColumns.add(queryColumn);
		
		QueryColumn queryColumn2 = new QueryColumn();
		queryColumn2.setField("saleState");
		queryColumn2.setVal(LeaseConstants.上架状态.getCode());
		queryColumns.add(queryColumn2);
		
		QueryColumn queryColumn3= new QueryColumn();
		queryColumn3.setField("eqState");
		queryColumn3.setVal(LeaseConstants.设备状态_闲置.getCode());
		queryColumns.add(queryColumn);

		dataCenter.setQueryForm(queryColumns);
		return lEqInfoController.page(dataCenter);
	}
	
	/**
	 * 查询物资分类信息
	 * @return
	 */
	@GetMapping(value = "/queryWzType")
	public Result<?> queryWzType() {
		LambdaQueryWrapper<LWzType> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(LWzType::getIsUse, LeaseConstants.是.getCode());
		queryWrapper.orderByAsc(LWzType::getTypeOrder);
		return Result.ok(iLWzTypeService.list(queryWrapper));
	}
	
	/**
	 * 根据物资分类，查询分类下的物资信息
	 * @param pageNo
	 * @param eqType
	 * @return
	 */
	@GetMapping(value = "/queryPageWzInfoByType")
	public Result<?> queryPageWzInfoByType(@RequestParam(name="pageNo",required=true) String pageNo,@RequestParam(name="wzType",required=true) String wzType) {
		
		DataCenter dataCenter = new DataCenter();
		dataCenter.setPageNo(Integer.parseInt(pageNo));
		dataCenter.setPageSize(15);
		List<QueryColumn> orderColumns = new ArrayList<>();
		QueryColumn orderColumn = new QueryColumn();
		orderColumn.setField("createTime");
		orderColumn.setVal("desc");
		orderColumns.add(orderColumn);
		dataCenter.setOrderColumns(orderColumns);
		
		List<QueryColumn> queryColumns = new ArrayList<>();
		QueryColumn queryColumn = new QueryColumn();
		queryColumn.setField("wzType");
		queryColumn.setVal(wzType);
		queryColumns.add(queryColumn);
		
		QueryColumn queryColumn2 = new QueryColumn();
		queryColumn2.setField("saleState");
		queryColumn2.setVal(LeaseConstants.上架状态.getCode());
		queryColumns.add(queryColumn2);

		dataCenter.setQueryForm(queryColumns);
		return lWzInfoController.page(dataCenter);
	}
	
	
	@PostMapping(value = "/leaseWzInfo")
	public Result<?> leaseWzInfo(@RequestBody LWzLeaseInfo wzLeaseInfo) {
		return lWzLeaseInfoController.save(wzLeaseInfo);
	}
	
	@PostMapping(value = "/leaseEqInfo")
	public Result<?> leaseEqInfo(@RequestBody LEqLeaseInfo eqLeaseInfo) {
		return lEqLeaseInfoController.save(eqLeaseInfo);
	}
	
	/**
	 * 根据参数编码查询参数信息
	 * @param paramCode
	 * @return
	 */
	@GetMapping(value = "/getSysConfigByCode")
	public Result<?> getSysConfigByCode(@RequestParam(name="paramCode",required=true) String paramCode) {
		return lSysConfigController.getSysConfigByCode(paramCode);
	}
	
}
